// pages/index/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {




    searchVal: ''

  },


  postSearch() {
    const that = this;
    that.getIndex()
  },

  bindsearch(e) {
    const that = this;
    that.setData({
      searchVal: e.detail.value.trim()
    })
  },

  bindSwiper(e) {
    this.setData({
      current: e.detail.current
    })
  },

  goods_type(e) {
    var that = this;
    var typeId = e.currentTarget.dataset.typeid;
   app.globalData.typeId = typeId
    wx.switchTab({
      url: '../goods/goods-list',
    })
  },

  navTo(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  getIndex() {
    const that = this;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'index',
      data: {
        search: that.data.searchVal
      },
      method: 'POST',
      success: function (res) {
        if (res.data.info) {
          that.setData({
            info: res.data.info,
            goods: res.data.info.hot_goods,
            searchVal: '',
          })
        }
        setTimeout(() => {
          wx.hideLoading()
        }, 500)
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.getIndex();
    wx.stopPullDownRefresh();

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (res) {
    const that = this
    var goods = that.data.goods;
    var page = goods.length - 1;
    wx.showLoading({
      title: '玩命加载中',
    })
    wx.request({
      url: app.globalData.url + 'index',
      data: {
        page: page
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        if (res.data.info.hot_goods.length != 0) {
          goods = goods.concat(res.data.info.hot_goods)
        } else {
          wx.hideLoading();
          wx.showToast({
            title: '暂无更多！',
            icon: 'none',
            mask: true,
          })
        }
        that.setData({
          goods: goods,
        })
      },
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})